package download

import (
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"novelDownlaod/config"
	"novelDownlaod/model"
	"strings"
)

func GetDownloadUrl(p model.Select) (string,error) {
	url:=config.Url+p.Url
	res,err:=http.Get(url)
	if err != nil {
		return "",err
	}
	defer res.Body.Close()
	doc,err:=goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		return "",err
	}
	//attr, exists := doc.Find(".downButton").Eq(2).Attr("href")
	html, err := doc.Find("body > div:nth-child(4) > div.show > div:nth-child(1) > div > div.detail_pic").Html()
	if err != nil {
		return "",err
	}
	arr:=strings.Split(html,"/")
	downloadUrl :=config.DownloadUrl+"/"+arr[4]+"/"+arr[5]+"/"+p.BookName+".txt"
	//if exists {
	//	return attr
	//}
	return downloadUrl,nil
}
func GetDownloadUrl2(p model.Select)(string,error)  {
    res,err:=http.Get(p.Url)
	if err != nil {
		return "",err
	}
	defer res.Body.Close()
    doc,err:=goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		return "",err
	}
	attr, exists := doc.Find("#xiashu > strong > a").Attr("href")
	if exists {
		res,err:=http.Get(config.Url2+attr)
		if err != nil {
			return "",err
		}
		defer res.Body.Close()

	}
	return "",nil

}
